﻿$import( "SmitearWebGL.Resources.Texture" );
$import( "SmitearWebGL.Events.ResourceEvent" );
$import( "SmitearWebGL.Resources.RenderSurface" );

SmitearWebGL.Resources.Texture2D = SmitearWebGL.Resources.Texture.extend(
{
	/******** constructor ********/
	
	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Resources.Texture2D" );
		
		/******** private property ********/

		this._renderSurfaces = null;
		this._glTextureTarget = null;
	}
	,
	
	/******** public method ********/
	
	load : function( fileName )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		var bitmap = this.context.resourceManager.createResource( "bitmap_" + fileName, SmitearWebGL.Resources.Bitmap );
		bitmap.addEventListener( SmitearWebGL.Resources.Bitmap.LOAD_COMPLETE, this, this._loadCompleteHandler);
		bitmap.load( fileName );
	}
	,
	getRenderSurface : function( mip_level )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		if ( ! this._renderSurfaces[mip_level] )
		{
			var renderSurface = this.context.resourceManager.createResource( this.name + "_render_surface", SmitearWebGL.Resources.RenderSurface );
			renderSurface.createDepthSurface( this.getWidth(), this.getHeight() );
			renderSurface.bindRenderTarget( this, mip_level );
			this._renderSurfaces[mip_level] = renderSurface;
		}

		return this._renderSurfaces[mip_level];
	}
	,
	drawImage : function( source_img, source_mip, dest_mip,
					source_x, source_y,
					source_width, source_height,
					dest_x, dest_y,
					dest_width, dest_height )
	{
		this._super( source_img._canvas, source_mip, dest_mip,
					source_x, source_y,
					source_width, source_height,
					dest_x, dest_y,
					dest_width, dest_height );
	}
	,
//	createEmptyTexture : function( texWidth, texHeight, intformat, format, type )
//	{
//		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
//		
//		if ( !SmitearWebGL.Core.Math.isPowerOfTwo( texWidth ) ||
//			!SmitearWebGL.Core.Math.isPowerOfTwo( texHeight ) )
//		{
//			texWidth = SmitearWebGL.Core.Math.nearestPowerOfTwo( texWidth );
//			texHeight = SmitearWebGL.Core.Math.nearestPowerOfTwo( texHeight );
//		}
//		
//		type = this._getGLTexturePixelType( type );
//		intformat = this._getGLTextureFormat( intformat );
//		format = this._getGLTextureFormat( format );
//		
//		if ( this._glTexture ) this.context.gl.deleteTexture( this._glTexture );
//		this._glTexture = this.context.gl.createTexture();
//		this.context.gl.bindTexture( this.context.gl.TEXTURE_2D, this._glTexture );
////		try
////		{
////			this.context.gl.texImage2D( this.context.gl.TEXTURE_2D, 0, intformat, texWidth, texHeight, 0, format, type, null );
////		}
////		catch ( e )
////		{
//			var pixels = new Uint8Array( texWidth * texHeight * 4 );
//			this.context.gl.texImage2D( this.context.gl.TEXTURE_2D, 0, intformat, texWidth, texHeight, 0, format, type, pixels );
////		}
//		this._texWidth = texWidth;
//		this._texHeight = texHeight;
//		
//		this.levels = 0;
//		
//		this.context.gl.bindTexture( this._glTextureTarget, null );
//	}
//	,
	/******** inherit method ********/
	
	_createResource : function()
	{
		this._super();
		
		this._renderSurfaces = [];
		this._glTextureTarget = this.context.gl.TEXTURE_2D;
	}
	,
	_destroyResource : function()
	{
		this._super();
		
		this._renderSurfaces.splice( 0, this._renderSurfaces.length );
		this._renderSurfaces = null;
	}
	,
	_loadCompleteHandler : function( e )
	{
		var bitmap = e.sender;
		bitmap.removeEventListener(  SmitearWebGL.Resources.Bitmap.LOAD_COMPLETE, this._loadCompleteHandler );
		this.setFromBitmap( bitmap, null );
		
		this.dispatchEvent( new SmitearWebGL.Events.Event( SmitearWebGL.Resources.Texture.LOAD_COMPLETE ) );
	}
}
);